import { DataSet } from 'choerodon-ui/pro';
import Record from 'choerodon-ui/pro/lib/data-set/Record';

export const dateData = [
  {
    name: '周一',
    text: '星期一',
    value: '1',
  },
  {
    name: '周二',
    text: '星期二',
    value: '2',
  },
  {
    name: '周三',
    text: '星期三',
    value: '3',
  },
  {
    name: '周四',
    text: '星期四',
    value: '4',
  },
  {
    name: '周五',
    text: '星期五',
    value: '5',
  },
  {
    name: '周六',
    text: '星期六',
    value: '6',
  },
  {
    name: '周天',
    text: '星期天',
    value: '7',
  },
];

const periodData = [
  {
    name: '10分钟',
    value: '10',
  },
  {
    name: '20分钟',
    value: '20',
  },
  {
    name: '30分钟',
    value: '30',
  },
  {
    name: '40分钟',
    value: '40',
  },
  {
    name: '50分钟',
    value: '50',
  }, {
    name: '60分钟',
    value: '60',
  },
  {
    name: '120分钟',
    value: '120',
  },
  {
    name: '240分钟',
    value: '240',
  },
];

export default ({ existNameArr }: {existNameArr:string[]}): object => ({
  autoCreate: true,
  fields: [
    {
      name: 'name',
      label: '定时计划名称',
      required: true,
      maxLength: 30,
      validator: (value:string) => {
        if (existNameArr.includes(value)) {
          return '定时计划名称已存在';
        }
        return true;
      },
    },
    {
      name: 'triggerType',
      label: '定时触发方式',
      defaultValue: 'period',
    },
    {
      name: 'weekNumber',
      type: 'string',
      label: '日期选择',
      multiple: ',',
      textField: 'name',
      value: 'value',
      required: true,
      options: new DataSet({
        data: dateData,
      }),
    },
    {
      name: 'timeRange',
      label: '时间段',
      type: 'date',
      range: ['start', 'end'],
      format: 'HH',
      dynamicProps: {
        required: ({ record }:{record:Record}) => record?.get('triggerType') === 'period',
        ignore: ({ record }:{record:Record}) => record?.get('triggerType') === 'single',
      },
      validator: (value: { start: any; end: any; }, name:string, record: Record) => {
        if (record?.get('triggerType') === 'single') {
          return true;
        }
        if (!value.start || !value.end) {
          return '请选择完整时间段';
        }
        return true;
      },
    },
    {
      name: 'period',
      label: '执行间隔',
      textField: 'name',
      valueField: 'value',
      dynamicProps: {
        required: ({ record }:{record:Record}) => record?.get('triggerType') === 'period',
        ignore: ({ record }:{record:Record}) => record?.get('triggerType') === 'single',
      },
      options: new DataSet({
        data: periodData,
      }),
    },
    {
      name: 'executeTime',
      label: '执行时间',
      type: 'time',
      format: 'HH:mm',
      dynamicProps: {
        required: ({ record }:{record:Record}) => record?.get('triggerType') === 'single',
        ignore: ({ record }:{record:Record}) => record?.get('triggerType') === 'period',
      },
    },
  ],
});
